//
//  WBEnvironmentManager.m
//  WeatherBar
//
//  Created by Bert on 2024/6/7.
//

#import "WBEnvironmentManager.h"
#import "BaseNetRequest.h"
#import <MJExtension/MJExtension.h>

@interface WBEnvironmentManager ()

@property (nonatomic,strong,readwrite) WBEnvironment * environmentSetting;
/// 全局请求的 url
@property (nonatomic, copy, readwrite) NSString *environmentUrl;
/// H5的 baseurl
@property (nonatomic, copy, readwrite) NSString *environmentH5Url;
/// 联通统一认证服务条款
@property (nonatomic, copy, readwrite) NSString *certificateServicesUrl;
/// 隐私政策
@property (nonatomic, copy, readwrite) NSString *privacyPolicyUrl;
/// 用户服务协议
@property (nonatomic, copy, readwrite) NSString *userAgreementUrl;
/// 统计的url
@property (nonatomic, copy, readwrite) NSString *logBaseUrl;
/// 翻译的网关
@property (nonatomic, copy, readwrite) NSString *translateGateWay;
/// 下载图片的baseUrl
@property (nonatomic, copy, readwrite) NSString *imageBaseUrl;
@end


@implementation WBEnvironmentManager

+ (instancetype)shareManager
{
    static WBEnvironmentManager *instance ;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[super allocWithZone:NULL] init];
        [instance singletonInitlize];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [WBEnvironmentManager shareManager];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [WBEnvironmentManager shareManager];
}

- (void)singletonInitlize
{
    self.areaType = [self.environmentSetting.appEnvironmentAreaType unsignedIntegerValue];
    self.environmentType = [self.environmentSetting.appEnvironmentProductType unsignedIntegerValue];
}

- (void)setEnvironmentType:(AppEnvironmentType)environmentType
{
    _environmentType = environmentType;
    NSArray * environmentArray = self.environmentSetting.environments;
    // 超出数组范围默认测试环境
    if (self.environmentType > (environmentArray.count-1)) {
        _environmentType = AppEnvironmentTypeTest;
    }
    
    WBEnvironmentItem * item = environmentArray[environmentType];
    WBEnvironmentBaseUrls * currentUrl = item.baseUrls;
    self.environmentUrl = currentUrl.baseUrl;
    self.imageBaseUrl = currentUrl.imageBaseUrl;
    self.environmentH5Url = currentUrl.h5url;
    self.logBaseUrl = currentUrl.logBaseUrl;
    self.translateGateWay = currentUrl.translateGateWay;
    self.certificateServicesUrl = currentUrl.certificateServicesUrl;
    self.privacyPolicyUrl = currentUrl.privacyPolicyUrl;
    self.userAgreementUrl = currentUrl.userAgreementUrl;
    
}

#pragma mark - lazy load
- (WBEnvironment *)environmentSetting
{
    if (_environmentSetting == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"BaseUrlEnvironment" ofType:@"plist"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        _environmentSetting = [WBEnvironment mj_objectWithKeyValues:dict];
    }
    return _environmentSetting;
}

/// 检查版本更新
- (void)checkVersoinShowUpdateAlertView:(WBShowUpdateAlertViewBlock)block
{
    // 耗时的操作

}




@end
